package com.gzy.studentsociety.web;

import com.gzy.studentsociety.dao.*;
import com.gzy.studentsociety.entity.Member;
import com.gzy.studentsociety.entity.Society;
import com.gzy.studentsociety.entity.Student;
import com.sun.xml.internal.ws.handler.HandlerException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

/**
 * @version: java version 1.8
 * @Author: Chuan Liu
 * @description:
 * @date: 2024-10-28 16:07
 */
@WebServlet(name = "student", urlPatterns = "/student/*")
public class StudentServlet extends RouteServlet {
    // 获取操作dao实例
    private SocietyDao societyDao = new SocietyDaoImpl();
    private StudentDao studentDao = new StudentDaoImpl();
    private MemberDao memberDao = new MemberDaoImpl();

    public String login(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String account = req.getParameter("account");
        String password = req.getParameter("password");
        Student stu = null;
        Member member = null;

        try {
            stu = studentDao.login(account,password);
            if(stu != null){

                member = memberDao.queryByNumber(stu.getStudentNumber());
                System.out.println("login: "+member);
                HttpSession session = req.getSession();
                session.setAttribute("student",stu.getStudentNumber());
                session.setAttribute("position",member);
//            res.sendRedirect("/studentSociety_war_exploded/main.jsp");
                res.sendRedirect("/studentSociety_war_exploded/society/mainIndex");
            }else{
                req.getRequestDispatcher("/index.jsp").forward(req,res);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            handleException(throwables, req, res);
        }


        return "direct";
    }

    // 查询学生
    public String queryStudent(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        String name = req.getParameter("name");
        String number = req.getParameter("number");
//        System.out.println("name: "+name);
//        System.out.println("number: "+number);
        List<Student> students = null;
        HttpSession session = req.getSession();
        String stuId = (String)session.getAttribute("student");
        Society society = null;
        try {
            students = studentDao.queryStudent(name, number);
            society = societyDao.queryByStudentNumber(stuId);

            req.setAttribute("students", students);
            req.setAttribute("society",society);

            // 正常情况转发到指定页面
//            req.getRequestDispatcher("/member/addPage").forward(req, res);
//            res.sendRedirect(req.getContextPath() + "member/addPage");
            return "member/addStudent";
        } catch (SQLException e) {
            e.printStackTrace();
            req.setAttribute("exception", e);
            // 异常情况下转发到错误页面
//            req.getRequestDispatcher("/error.jsp").forward(req, res);
            return "/error.jsp";
        }

    }


    protected Class getMyClass() {
        return this.getClass();
    }
}
